#include <stdio.h>
#include "shmmanager.h"

const int KEY = 5259808; 

struct Test
{
	int a;
	int b;
};

int main(int argc,char * argv[])
{
	int ret = -1;
	int pid = -1;
	pid = fork();
	if ( pid == 0 )
	{
		printf("waiting father to init\n");
		sleep(5);
		ShmManager shmmng(KEY);
		shmmng.Creat( sizeof(Test) );	 
	
		Test * pTest = (Test *) shmmng.GetShm();
		
		printf("pid[%d] %d %d\n",getpid(),pTest->a,pTest->b);	
		pTest->a = 2;
		pTest->b = 2;
		printf("pid[%d] %d %d\n",getpid(),pTest->a,pTest->b);	
		shmmng.ShmDT();
	}
	else if (pid > 0)
	{
		ShmManager shmmng(KEY);
		shmmng.Creat( sizeof(Test) );	 
		Test * pTest = (Test *) shmmng.GetShm();
		pTest->a = 1;
		pTest->b = 1;
		printf("pid[%d] %d %d\n",getpid(),pTest->a,pTest->b);	
		printf("waiting child to change\n");
		sleep(5);
		printf("pid[%d] %d %d\n",getpid(),pTest->a,pTest->b);	
		shmmng.ShmDT();
		shmmng.Remove();
	}
	else
		printf("fork error\n");

	return ret;
}
